//
//  UIView+Extension.m
//  BTShike2
//
//  Created by 刘军林 on 2019/10/14.
//  Copyright © 2019 刘军林. All rights reserved.
//

#import "UIView+Extension.h"


@implementation UIView (Extension)

-(UIImage *) shikeMainColorView
{
    CAGradientLayer *gradientLayer = [CAGradientLayer layer];
    gradientLayer.frame = self.bounds;

    UIView *bgView = [[UIView alloc] initWithFrame:self.bounds];

    gradientLayer.colors = @[(__bridge id)[UIColor redColor].CGColor , (__bridge id)[UIColor yellowColor].CGColor ];
    gradientLayer.startPoint = CGPointMake(0, 0.5);
    gradientLayer.endPoint = CGPointMake(1, 0.5);
    gradientLayer.locations = @[@(0.f), @(1.f)];
    [bgView.layer addSublayer:gradientLayer];

    return [bgView convertViewToImage];
}

-(UIImage *)convertViewToImage{
    CGSize s = self.bounds.size;
    // 下面方法，第一个参数表示区域大小。第二个参数表示是否是非透明的。如果需要显示半透明效果，需要传NO，否则传YES。第三个参数就是屏幕密度了
    UIGraphicsBeginImageContextWithOptions(s, YES, [UIScreen mainScreen].scale);
    [self.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage*image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}

-(UIImage * ) colorImage:(UIColor *)startColor endColor:(UIColor *)endColor
{
    CAGradientLayer *gradientLayer = [CAGradientLayer layer];
    gradientLayer.frame = self.bounds;
    
    UIView *bgView = [[UIView alloc] initWithFrame:self.bounds];
    
    gradientLayer.colors = @[(__bridge id)startColor.CGColor , (__bridge id)endColor.CGColor];
    gradientLayer.startPoint = CGPointMake(0, 0.5);
    gradientLayer.endPoint = CGPointMake(1, 0.5);
    gradientLayer.locations = @[@(0.f), @(1.f)];
    [bgView.layer addSublayer:gradientLayer];
    
    return [bgView convertViewToImage];
}

-(UIImage * ) verticalColorImage:(UIColor *) startColor endColor:(UIColor *)endColor
{
    CAGradientLayer *gradientLayer = [CAGradientLayer layer];
    gradientLayer.frame = self.bounds;
    
    UIView *bgView = [[UIView alloc] initWithFrame:self.bounds];
    
    gradientLayer.colors = @[(__bridge id)startColor.CGColor , (__bridge id)endColor.CGColor];
    gradientLayer.startPoint = CGPointMake(0.5, 0.0);
    gradientLayer.endPoint = CGPointMake(0.5, 1.0);
    gradientLayer.locations = @[@(1.f), @(1.f)];
    [bgView.layer addSublayer:gradientLayer];
    
    return [bgView convertViewToImage];
}

@end
